package com.atguigu.gmall.common.config.exception.handler;

import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName GlobalExceptionAdvice
 * @Description TODO
 * @Author king
 * @Date 2022/11/20 0:55
 * @Version 1.0
 */
@Slf4j
@ResponseBody
@Component
@RestController
public class GlobalExceptionHandler {

    @ExceptionHandler(GmallException.class)
    public Result handleGmallException(GmallException exception){
        Result<Object> fail = Result.fail();
        fail.setCode(exception.getCode());
        log.error("错误: {}", exception);
        fail.setMessage(exception.getMessage());

        return fail;
    }

    @ExceptionHandler(GmallException.class)
    public Result handleException(Throwable throwable){
        Result<Object> fail = Result.fail();
        fail.setMessage("服务器内部错误");
        log.error("错误: {}", throwable);
        //fail.setData(throwable.getStackTrace());
        return fail;
    }

}
